package com.framework.modules.job.task;

import com.framework.entity.flow.FlowAccount;
import com.framework.entity.sys.SysActive;
import com.framework.entity.sys.SysActiveAgent;
import com.framework.entity.sys.SysActiveInfo;
import com.framework.entity.sys.SysAgent;
import com.framework.modules.sys.controller.SysActiveAgentController;
import com.framework.service.*;
import com.framework.utils.DateUtils;
import com.framework.utils.string.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component("xinChuangActiveBalanceTask")
public class XinChuangActiveBalanceTask {

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private ActiveService activeService;
    @Autowired
    private SysActiveInfoService sysActiveInfoService;
    @Autowired
    private ActiveAgentService activeAgentService;
    @Autowired
    private TerminalInfoService terminalInfoService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private FlowAccountService flowAccountService;

    Long deptId=889324L;

    public  void  balanceXinChuangActive(){
        logger.info("------------信创活动结算开始--------------");
        List<SysActive> list=activeService.queryActiveList(deptId);

        List<SysActive> collect = list.stream().filter(e -> e.getType().intValue() == 4).collect(Collectors.toList());
        for (SysActive active : collect) {
            handleTeamAward(active.getActiveId());
        }
        logger.info("------------信创活动结算成功--------------");
    }
    //处理团队奖励
    private void handleTeamAward(Long activeId) {
        //所有信创的合伙人
        List<SysAgent> agents=agentService.queryNormalAgentList(deptId);
        //进行中的活动详情
        List<SysActiveInfo>  activesList=sysActiveInfoService.queryOnActiveInfoByActiveId(activeId);

        for (SysAgent sysAgent :agents) {

            countActiveNum(sysAgent.getAgentId(),activesList,activeId);
        }
        handleActiveAmount();//结算所有活动奖励
    }
    @Transactional(rollbackFor = Exception.class)
    public void handleActiveAmount() {
        logger.info("-----------结算所有团队奖励开始-----------");
        //查出所有未结算已达标的记录
        List<SysActiveAgent> list=activeAgentService.queryAllNoBalance();
        SysActiveInfo sysActiveInfo;
        for (SysActiveAgent sysActiveAgent :list) {

            sysActiveInfo=sysActiveInfoService.queryActiveInfoById(sysActiveAgent.getActiveInfoId());

            agentInfoService.addAmount(sysActiveAgent.getAgentId(),sysActiveInfo.getMoney());
            sysActiveAgent.setIsBalance(2);
            sysActiveAgent.setAwardDate(new Date());
            activeAgentService.updateById(sysActiveAgent);
            //添加流水日志
            FlowAccount account = new FlowAccount();
            account.setCreateTime(new Date());
            account.setAccountType(10);
            account.setBelId(sysActiveAgent.getActiveAgentId().toString());
            account.setSupportType(2);
            account.setAmount(sysActiveInfo.getMoney());
            account.setAgentId(sysActiveAgent.getAgentId());
            flowAccountService.insertFlowAccount(account);
        }
        logger.info("-----------结算所有团队奖励成功-----------");
    }

    private void countActiveNum(Long agentId, List<SysActiveInfo> activesList,Long activeId) {
        logger.info("-----------正在处理合伙人id为："+agentId+"的活动-----------");
        Date today=new Date();
        //查询个人的直属合伙人数量
        //本月的数量 2.上个月
        int type=2;
        List<SysAgent> subAgentList = agentService.queryRealSubAgent(agentId,type);
        Integer realSubNum=subAgentList.size();
        //查询有效激活机具
        Integer realActivityNum=0;
        int count;
        for (SysAgent tempSubAgent : subAgentList) {

            count = terminalInfoService.queryActivationCount(tempSubAgent.getAgentId(), 2, 1);
            if(count>0){
                realActivityNum++;
            }
        }
        SysActiveAgent sysActiveAgent=activeAgentService.queryNoBalanceActiveByAgentId(agentId,activeId);

        sysActiveAgent.setRealSubNum(realSubNum);
        sysActiveAgent.setRealActivityNum(realActivityNum);

        for (SysActiveInfo temp : activesList) {

            if (realSubNum>temp.getSubNum() && realActivityNum > temp.getActivityNum()) {
                sysActiveAgent.setActiveInfoId(temp.getActiveInfoId());
                sysActiveAgent.setState(1);
            }
        }
        //昨天
        sysActiveAgent.setSettleTime(new Date(today.getTime() - 86400000L));
        activeAgentService.updateById(sysActiveAgent);
        //如果活动依然进行，则添加新的记录
        if(activesList.get(0).getEndDate().after(new Date(today.getTime() + 86400000L))){
            activeAgentService.insertOwnerActive(agentId,activeId,activesList.get(0).getActiveInfoId());
        }
        logger.info("-----------合伙人id为："+agentId+"的活动处理完毕-----------");
    }

}
